package com.koi.common.utils.ip;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import org.springframework.stereotype.Component;

/**
 * @author ･ᴗ･
 * @description [IpAddressUtil]
 */
@Slf4j
@Component
public class IpAddressUtil {

    @Resource
    private Ip2regionSearcher ip2regionSearcher;

    // 未知地址
    public static final String UNKNOWN = "未知地理位置";

    /**
     * @author ･ᴗ･
     * @description [查询IP地址]
     */
    public String getAddress(String ip) {
        // 内网不查询
        if (IpUtil.internalIp(ip)) {
            return "内网IP，无法获取位置";
        }
        try {
            // 获取地址
            return ip2regionSearcher.getAddress(ip);
        } catch (Exception e) {
            log.error("获取地理位置异常 {}", ip);
        }
        return UNKNOWN;
    }

}
